package com.dhcc.utils.string;

import com.dhcc.utils.Operator;
import com.dhcc.utils.OperatorException;

/**
 * support bwtween "head" "tail"
 * 
 * @author hibean@qq.com 2017年6月28日
 *
 */
public class BetweenOperator implements Operator<String> {

	@Override
	public Object invoke(String operator, String first, Object[] args) {
		try{
			String head = (String) args[0];
			String tail = (String) args[1];
			int index1 = first.indexOf(head);
			int index2 = first.indexOf(tail, index1+head.length());
			if (index1 >= 0 && index2 > index1) {
				return first.substring(index1+head.length(), index2);
			}
			throw new OperatorException("未在 “"+first+"” 找到介于 “"+head+"” 与 “"+ tail+"” 之间的内容");
		} catch(OperatorException ex){
			throw ex;
		} catch(Exception ex){
			throw new OperatorException("between 用法错误", ex);
		}
	}
	
}
